package br.com.datasun.biblioteca.ui;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;

import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextField;
import javax.swing.JButton;

import images.ImageLoader;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;
import java.awt.FlowLayout;

import br.com.datasun.biblioteca.entities.PessoaFisica;
import br.com.datasun.biblioteca.ui.components.ConsultaClienteTableModel;
import br.com.datasun.biblioteca.ui.controller.ConsultaClienteController;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;


public class ConsultaClientesUI extends JDialog{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel jlTitulo = new JLabel("Consulta de Clientes");
	private JPanel jpCabecalhoConsultaClientes = new JPanel();
	private JPanel jpDetalhesConsultaClientes = new JPanel();
	private JPanel jpCabecallhoConsultaClientesBotoes = new JPanel();
	private JPanel jpCabecalhoConsultaClientesDados = new JPanel();
	private JPanel jpDetalhesConsultaClientesBotoes = new JPanel();
	private JPanel jpDetalhesConsultaClientesTabela = new JPanel();
	private JLabel jlNome = new JLabel("Nome");
	private JTextField jtNome = new JTextField();
	private JLabel jlEmail = new JLabel("Email");
	private JTextField jtEmail = new JTextField();
	private final JButton jbPesquisar = new JButton("");
	private final JButton jbLimpar = new JButton("");
	private final JPanel jpItensConsultaClientes = new JPanel();
	private final JPanel jpItensConsultaClientesBotoes = new JPanel();
	private final JButton jbSelecionar = new JButton("");
	private final JButton jbCancelar = new JButton("");
	private final JPanel jpItensConsultaClientesTabela = new JPanel();
	
	private JScrollPane jscrollPane = new JScrollPane();
	private JTable tabelaConsultaClientes = new JTable();
	private ConsultaClienteTableModel model = new ConsultaClienteTableModel();
	
	private PessoaFisica clienteSelecionado = new PessoaFisica();
	private List<PessoaFisica> clientes = new ArrayList<PessoaFisica>();
	
	private ConsultaClienteController controller;
	private final JLabel jlRG = new JLabel("RG");
	private final JTextField jtRG = new JTextField();
	
	private JPanel panel = new JPanel();
	
	private MaskFormatter cpfMaskFormater;	
	private final JLabel label = new JLabel("CPF");
	private final JFormattedTextField jtCPF = new JFormattedTextField();

	/**
	 * Create the panel.
	 */
	public ConsultaClientesUI() {
		try{
			inicializar();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public ConsultaClientesUI(PessoaFisica clienteSelecionado) {
		try{
			clienteSelecionado = this.clienteSelecionado;
			inicializar();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void inicializar()throws Exception{
		setBounds(100, 100, 581, 462);
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(panel, BorderLayout.CENTER);
		
			
			Dimension tamanhoBotoes = new Dimension(115,25);
			
			jlTitulo.setPreferredSize(new Dimension(40,40));
			jlTitulo.setFont(new Font("Tahoma", Font.BOLD, 11));
			jlTitulo.setHorizontalAlignment(SwingConstants.CENTER);
			getContentPane().add(jlTitulo, BorderLayout.NORTH);
			jtNome.setPreferredSize(new Dimension(200,20));
			
			
			
			panel.setLayout(new BorderLayout(0, 0));
			
			jpCabecalhoConsultaClientes.setPreferredSize(new Dimension(40, 155));
			panel.add(jpCabecalhoConsultaClientes, BorderLayout.NORTH);
			jpCabecalhoConsultaClientes.setLayout(new BorderLayout(0, 0));
			FlowLayout flowLayout = (FlowLayout) jpCabecallhoConsultaClientesBotoes.getLayout();
			flowLayout.setHgap(10);
			flowLayout.setAlignment(FlowLayout.RIGHT);
			
			jpCabecallhoConsultaClientesBotoes.setPreferredSize(new Dimension(50, 35));
			jpCabecalhoConsultaClientes.add(jpCabecallhoConsultaClientesBotoes, BorderLayout.SOUTH);
			jbLimpar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					controller.btnLimparEventHandler();
				}
			});
			jbLimpar.setPreferredSize(tamanhoBotoes);
			jbPesquisar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					controller.btnPesquisarEventHandler();
				}
			});
			jbPesquisar.setPreferredSize(tamanhoBotoes);
			
			jbLimpar.setIcon(ImageLoader.createImageIcon("btnLimpar115x25.png"));
			jbPesquisar.setIcon(ImageLoader.createImageIcon("btnPesquisar115x25.png"));
			
			jpCabecallhoConsultaClientesBotoes.add(jbLimpar);
			
			jpCabecallhoConsultaClientesBotoes.add(jbPesquisar);
			
			jpCabecalhoConsultaClientes.add(jpCabecalhoConsultaClientesDados, BorderLayout.CENTER);
			GridBagLayout gbl_jpCabecalhoConsultaClientesDados = new GridBagLayout();
			gbl_jpCabecalhoConsultaClientesDados.columnWidths = new int[]{60, 205, 66, 159, 60, 0};
			gbl_jpCabecalhoConsultaClientesDados.rowHeights = new int[]{30, 30, 30, 0};
			gbl_jpCabecalhoConsultaClientesDados.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
			gbl_jpCabecalhoConsultaClientesDados.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
			jpCabecalhoConsultaClientesDados.setLayout(gbl_jpCabecalhoConsultaClientesDados);
			
			GridBagConstraints gbc_jlNome = new GridBagConstraints();
			gbc_jlNome.anchor = GridBagConstraints.EAST;
			gbc_jlNome.insets = new Insets(0, 0, 5, 5);
			gbc_jlNome.gridx = 0;
			gbc_jlNome.gridy = 1;
			jlNome.setFont(new Font("Tahoma", Font.BOLD, 11));
			jpCabecalhoConsultaClientesDados.add(jlNome, gbc_jlNome);
			
			GridBagConstraints gbc_jtNome = new GridBagConstraints();
			gbc_jtNome.fill = GridBagConstraints.HORIZONTAL;
			gbc_jtNome.insets = new Insets(0, 0, 5, 5);
			gbc_jtNome.gridx = 1;
			gbc_jtNome.gridy = 1;
			jpCabecalhoConsultaClientesDados.add(jtNome, gbc_jtNome);
			
			GridBagConstraints gbc_jlEmail = new GridBagConstraints();
			gbc_jlEmail.anchor = GridBagConstraints.EAST;
			gbc_jlEmail.insets = new Insets(0, 0, 5, 5);
			gbc_jlEmail.gridx = 2;
			gbc_jlEmail.gridy = 1;
			jlEmail.setFont(new Font("Tahoma", Font.BOLD, 11));
			jpCabecalhoConsultaClientesDados.add(jlEmail, gbc_jlEmail);
			
			GridBagConstraints gbc_jtEmail = new GridBagConstraints();
			gbc_jtEmail.fill = GridBagConstraints.HORIZONTAL;
			gbc_jtEmail.insets = new Insets(0, 0, 5, 5);
			gbc_jtEmail.gridx = 3;
			gbc_jtEmail.gridy = 1;
			jtEmail.setPreferredSize(new Dimension(100, 20));
			jpCabecalhoConsultaClientesDados.add(jtEmail, gbc_jtEmail);
			
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.insets = new Insets(0, 0, 0, 5);
			gbc_label.anchor = GridBagConstraints.EAST;
			gbc_label.gridx = 0;
			gbc_label.gridy = 2;
			label.setFont(new Font("Tahoma", Font.BOLD, 11));
			jpCabecalhoConsultaClientesDados.add(label, gbc_label);
			
			GridBagConstraints gbc_jtCPF = new GridBagConstraints();
			gbc_jtCPF.insets = new Insets(0, 0, 0, 5);
			gbc_jtCPF.fill = GridBagConstraints.HORIZONTAL;
			gbc_jtCPF.gridx = 1;
			gbc_jtCPF.gridy = 2;
			jtCPF.setPreferredSize(new Dimension(100, 20));
			jpCabecalhoConsultaClientesDados.add(jtCPF, gbc_jtCPF);
			
			GridBagConstraints gbc_jlRG = new GridBagConstraints();
			gbc_jlRG.anchor = GridBagConstraints.EAST;
			gbc_jlRG.insets = new Insets(0, 0, 0, 5);
			gbc_jlRG.gridx = 2;
			gbc_jlRG.gridy = 2;
			jlRG.setFont(new Font("Tahoma", Font.BOLD, 11));
			jpCabecalhoConsultaClientesDados.add(jlRG, gbc_jlRG);
			
			GridBagConstraints gbc_jtRG = new GridBagConstraints();
			gbc_jtRG.fill = GridBagConstraints.HORIZONTAL;
			gbc_jtRG.insets = new Insets(0, 0, 0, 5);
			gbc_jtRG.gridx = 3;
			gbc_jtRG.gridy = 2;
			jtRG.setPreferredSize(new Dimension(100, 20));
			jpCabecalhoConsultaClientesDados.add(jtRG, gbc_jtRG);
			
			panel.add(jpItensConsultaClientes, BorderLayout.CENTER);
			jpItensConsultaClientes.setLayout(new BorderLayout(0, 0));
			FlowLayout flowLayout_1 = (FlowLayout) jpItensConsultaClientesBotoes.getLayout();
			flowLayout_1.setAlignment(FlowLayout.RIGHT);
			
			jpItensConsultaClientesBotoes.setPreferredSize(new Dimension(40,40));
			
			jpItensConsultaClientes.add(jpItensConsultaClientesBotoes, BorderLayout.SOUTH);
			jbCancelar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					controller.btnFecharEventHandler();
				}
			});
			
			jbCancelar.setPreferredSize(tamanhoBotoes);
			jbSelecionar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					controller.btnConfirmarEventHandler();
				}
			});
			jbSelecionar.setPreferredSize(tamanhoBotoes);
			jbCancelar.setIcon(ImageLoader.createImageIcon("btnCancelar115x25.png"));
			jbSelecionar.setIcon(ImageLoader.createImageIcon("btnSelecionar115x25.png"));
			
			jpItensConsultaClientesBotoes.add(jbCancelar);
			
			jpItensConsultaClientesBotoes.add(jbSelecionar);
			
			jpItensConsultaClientes.add(jpItensConsultaClientesTabela, BorderLayout.CENTER);
			jpItensConsultaClientesTabela.setLayout(new BorderLayout(20, 20));
			
			
			tabelaConsultaClientes.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					int quantidadeDeCliques = arg0.getClickCount();
					if(quantidadeDeCliques >= 2){
						controller.btnConfirmarEventHandler();			
					}
				}
			});
			
			jtNome.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent arg0) {
					char tecla = arg0.getKeyChar();
					if ((tecla == KeyEvent.VK_ENTER)&&(jtNome.getText().length() > 0)){
						controller.btnPesquisarEventHandler();
					}
				}
			});
			
			jtEmail.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent arg0) {
					char tecla = arg0.getKeyChar();
					if ((tecla == KeyEvent.VK_ENTER)&&(jtEmail.getText().length() > 0)){
						controller.btnPesquisarEventHandler();
					}
				}
			});
			
			jtRG.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent arg0) {
					char tecla = arg0.getKeyChar();
					if ((tecla == KeyEvent.VK_ENTER)&&(jtRG.getText().length() > 0)){
						controller.btnPesquisarEventHandler();
					}
				}
			});
			
			jtCPF.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent arg0) {
					char tecla = arg0.getKeyChar();
					if ((tecla == KeyEvent.VK_ENTER)&&(jtCPF.getText().length() > 0)){
						controller.btnPesquisarEventHandler();
					}
				}
			});

			
			
			jscrollPane.setViewportView(tabelaConsultaClientes);
			jpItensConsultaClientesTabela.add(jscrollPane);
			
			tabelaConsultaClientes.setModel(model);			
			
			cpfMaskFormater = new MaskFormatter("###.###.###-##");
			cpfMaskFormater.setPlaceholderCharacter('_');
			jpDetalhesConsultaClientes.add(jpDetalhesConsultaClientesTabela, BorderLayout.CENTER);
						
			jpDetalhesConsultaClientes.setLayout(new BorderLayout(0, 0));
			
			jpDetalhesConsultaClientesBotoes.setPreferredSize(new Dimension(50,50));
			jpDetalhesConsultaClientes.add(jpDetalhesConsultaClientesBotoes, BorderLayout.SOUTH);
			
			controller = new ConsultaClienteController(this);
			
			editarCampos(true);
			
			

		}
	
	public void limparFormulario(){		
		
		jtEmail.setText("");
		jtCPF.setText("");
		jtNome.setText("");
		jtRG.setText("");
		
	}
	
	public void editarCampos(boolean editar){
		jbLimpar.setEnabled(editar);
		jbPesquisar.setEnabled(editar);
		jbSelecionar.setEnabled(editar);
		jbCancelar.setEnabled(editar);
		
	}
	
	public boolean setClienteSelecionado() {
		boolean result = false;
		this.clienteSelecionado = model.getClienteDe(getIndiceClienteSelecionado());
		if (clienteSelecionado != null){
			result = true;
		}
		return result;
	}

	public PessoaFisica getClienteSelecionado() {		
		return clienteSelecionado;
	}
	
	public void limparClienteSelecionado(){
		clienteSelecionado = null;
		jtNome.setText("");
		jtEmail.setText("");
		jtCPF.setText("");
		jtRG.setText("");
	}
	
	public String getNome(){
		return jtNome.getText();
	}
	
	public String getEmail(){
		return jtEmail.getText();
	}
	
	public String getCPF(){
		return jtCPF.getText();
	}	

	public String getRG(){
		return jtRG.getText();
	}
	
	public void setClientes(List<PessoaFisica> clientes){
		this.model.setConsultaCliente(clientes);
	}
	
	public int getIndiceClienteSelecionado() {
		return tabelaConsultaClientes.getSelectedRow();
	}
	
	public PessoaFisica pesquisarClientes(Component c){
		return controller.pesquisarClientes(c);
	}

}
